package com.online.demo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.online.demo.common.CommonStr;
import com.online.demo.entity.ContentSolrEntity;
import com.online.demo.entity.TContent;
import com.online.demo.entity.TType;
import com.online.demo.mapper.TContentMapper;
import com.online.demo.service.ISolrService;
import lombok.extern.slf4j.Slf4j;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@Slf4j
public class SolrServiceImpl implements ISolrService {

    @Autowired
    private SolrTemplate solrTemplate;

    @Resource
    private TContentMapper contentMapper;

    // 添加一条  -- 先插入数据库然后再插入 solr,  插入数据库之后id会回填，
    @Override
    public int addOrUpdateContentToSolr(TContent content, TType type, String nickName) {
        if (content == null) {
            return 1;
        }

        ContentSolrEntity solrEntity = new ContentSolrEntity();

        solrEntity.setId(content.getId());
        solrEntity.setTitle(content.getTitle());
        solrEntity.setImg(content.getImg());
        solrEntity.setCreateTime(System.currentTimeMillis());
        solrEntity.setTypeName(type.getTypename());
        solrEntity.setPTypeName(type.getPtypename());
        solrEntity.setUserNickName(nickName);
        solrEntity.setBriefInfo(content.getBriefinfo());
        solrEntity.setViewNum(0);
        solrEntity.setLikeNum(0);

        UpdateResponse response = solrTemplate.saveBean(CommonStr.COLLECTION_CONTENT, solrEntity);
        solrTemplate.commit(CommonStr.COLLECTION_CONTENT);

        return response.getStatus();
    }

    // 添加多条
    @Override
    public int addOrUpdateContentListToSolr(List<ContentSolrEntity> contentList) {
        if (contentList == null || contentList.size() == 0) {
            return 1;
        }

        for (ContentSolrEntity content : contentList) {
            UpdateResponse response = solrTemplate.saveBean(CommonStr.COLLECTION_CONTENT, content);
            if (response.getStatus() != 0) {
                log.info("content 插入失败{}");
                continue;
            }
        }
        solrTemplate.commit(CommonStr.COLLECTION_CONTENT);
        return 0;
    }


    // 根据id删除
    @Override
    public int deleteContentById(String id) {

        UpdateResponse response = solrTemplate.deleteByIds(CommonStr.COLLECTION_CONTENT, id);

        return response.getStatus();
    }


    // 根据id删除多个
    @Override
    public int deleteContentByIdList(List<String> idList) {

        UpdateResponse response = solrTemplate.deleteByIds(CommonStr.COLLECTION_CONTENT, idList);

        return response.getStatus();
    }


    // 关键字查询 -- title -- type -- info 中包含的字段
    @Override
    public List<ContentSolrEntity> findContentByKeyWords(String keywords) {

        SimpleQuery query = new SimpleQuery();

        query.addCriteria(new Criteria("item_keywords").is(keywords));

        query.addSort(Sort.by("item_createTime").descending());

        List<ContentSolrEntity> contentSolrEntityList = solrTemplate.query(CommonStr.COLLECTION_CONTENT, query, ContentSolrEntity.class).getContent();

        return contentSolrEntityList;

    }


    // 浏览次数 + 1 ， 每浏览10次，向数据库中写入一次
    public int viewNumAddOne(int id){
        ContentSolrEntity solrEntity = solrTemplate.getById(CommonStr.COLLECTION_CONTENT, id, ContentSolrEntity.class).get();

        if (solrEntity != null) {
            int viewNum = solrEntity.getViewNum() + 1;
            solrEntity.setViewNum( viewNum);
            if (viewNum % 10 == 0) {    // 每10浏览次写入数据库一次
                contentMapper.updateViewNum(id, viewNum);
            }

            // 重新写入solr
            UpdateResponse response = solrTemplate.saveBean(CommonStr.COLLECTION_CONTENT, solrEntity);
            solrTemplate.commit(CommonStr.COLLECTION_CONTENT);
            return response.getStatus();

        }

        return 1;
    }

    // 分页查询
    @Override
    public PageInfo<ContentSolrEntity> findContentByKeyWordsForPage(Integer index, Integer size, String keywords) {
        List<ContentSolrEntity> contentSolrEntityList = findContentByKeyWords(keywords);
        PageInfo<ContentSolrEntity> pageInfo = new PageInfo<>(contentSolrEntityList, 5);
        return pageInfo;
    }
}
